package nl.rainbowsheep.reisorg.empl.site.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import nl.rainbowsheep.reisorg.empl.site.authentication.BasicAuthInterceptor;
import nl.rainbowsheep.reisorg.empl.site.model.Accommodation;
import nl.rainbowsheep.reisorg.empl.site.viewmodel.AccommodationViewmodel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

/**
 * Service class for Accomodation
 */
@Service
public class AccommodationService {

    @Value("${api.url}")
    String url;

    public List<AccommodationViewmodel> getAll() {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);

        List<AccommodationViewmodel> accommodations = template.getForObject(url + "/accommodation/getall?" + getLocaleParam(), List.class);
        return accommodations;
    }

    public List<AccommodationViewmodel> getSearchResult(AccommodationViewmodel accommodationViewmodel) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        try {
            AccommodationViewmodel[] models = template.postForObject(url + "/accommodation/find", accommodationViewmodel, AccommodationViewmodel[].class);
            return Arrays.asList(models);
        } catch (RestClientException ex) {
            return null;
        }
    }

    public Accommodation insertAccommodationViewmodelNL(AccommodationViewmodel accommodationViewmodel) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        return template.postForObject(url + "/accommodation/save?locale=nl", accommodationViewmodel, Accommodation.class);
    }

    public Accommodation insertAccommodationViewmodelEN(AccommodationViewmodel accommodationViewmodel) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        return template.postForObject(url + "/accommodation/save?locale=en", accommodationViewmodel, Accommodation.class);
    }

    /**
     * Get current locale (from cookie)
     *
     * @return string with locale param
     */
    private String getLocaleParam() {
        return "locale=" + LocaleContextHolder.getLocale().getLanguage();
    }

}
